#include "AnimEditorDlg.h"
#include "Animation.h"
#include "texture.h"
Animation::Animation()
{
	mCurframe = -1;
	mPassed = 0;
	mPlaytype = LOOP;
	mInterpolated = 0;
}

Animation::~Animation()
{

}

void Animation::Update( int diff )
{
	if( mCurframe < 0 || mCurframe >= mFrames.size() )
		return;
	Frame &f = mFrames[mCurframe];
	mPassed += diff;
	if( mPassed >= f.frametime )
	{
			mCurframe++;
			mPassed = 0;
	}
	if(mCurframe >= mFrames.size())
	{
		if( mPlaytype == ONCE )
			mCurframe = -1;
		else if( mPlaytype == LOOP )
			mCurframe = 0;
	}
}

void Animation::Render( int x, int y/*, int w, int h*/ )
{
	if( mCurframe < 0 || mCurframe >= mFrames.size() )
		return;
	Frame f = mFrames[mCurframe];
	extern CAnimEditorDlg *gDlg;
	gDlg->RenderTexture( f.texid, f.offsetx, f.offsety );
	for( int i=0;i<MAX_WP_BOX;i++ )
	{
		gDlg->ConvertBox( f, false, f.weaponbox[i] );
		CD3DManager::Instance().DrawRect( f.weaponbox[i], 0xffff0000 );
	}
	for( int i=0;i<MAX_CL_BOX;i++ )
	{
		gDlg->ConvertBox( f, false, f.collisionbox[i] );
		CD3DManager::Instance().DrawRect( f.collisionbox[i], 0xff00ff00 );
	}
	//extern int renderwidth,renderheight;
	//TextureInfo *info = CD3DManager::Instance().GetTextureInfo(f.texid);
	//float scalex = float(renderwidth)/float(info->width);
	//float scaley = float(renderheight)/float(info->height);
	//float scale = scalex < scaley?scalex:scaley;
	//CD3DManager::Instance().DrawTexture(f.texid, x, y, info->width*scale, info->height*scale  );
}

void Animation::Start()
{
	mCurframe = 0;
	mPassed = 0;
}
